﻿using Infrastructure.UI;
using InventoryManagementSystem.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using ZXing;
using ZXing.Common;
using ZXing.OneD;

namespace InventoryManagementSystem
{
    public sealed partial class PurchasePage : InventoryManagementSystem.Common.LayoutAwarePage
    {
        Order order = Order.Create();

        public PurchasePage()
        {
            this.InitializeComponent();

            this.EnablePrivacyPolicy(PrivacyPolicy.RunXiaDianQi);
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            DefaultViewModel["Order"] = order;
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void page_Loaded(object sender, RoutedEventArgs e)
        {
            productBarCodeTextBox.Focus(FocusState.Programmatic);
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!order.Validate())
            {
                MessageBox.Show("请检查输入");
                return;
            }

            DataStore.AddPurchaseOrder(order);

            discardButton_Click(sender, new RoutedEventArgs());
        }

        private void discardButton_Click(object sender, RoutedEventArgs e)
        {
            order.Reset();

            productBarCodeTextBox.Focus(FocusState.Programmatic);
        }

        private void productBarCodeTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                productNameTextBox.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void productNameTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                productUnitPriceTextBox.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void productUnitPriceTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                orderUnitPriceTextBox.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void orderUnitPriceTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                orderCountTextBox.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void orderCountTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                orderTotalPriceTextBox.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void orderTotalPriceTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                saveButton.Focus(FocusState.Programmatic);
                e.Handled = true;
            }
        }

        private void productBarCodeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (productBarCodeTextBox.Text.Length == 13)
            {
                var existedProduct = DataStore.GetProduct(productBarCodeTextBox.Text);

                if (existedProduct != null)
                {
                    order.Product = existedProduct;
                }
            }
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private async void scanBarCode_Click(object sender, RoutedEventArgs e)
        {
            await Launcher.LaunchUriAsync(new Uri("http://item.taobao.com/item.htm?spm=686.1000925.1000774.15.vBDKsK&id=18235679080"));
        }

        private async void createBarCode_Click(object sender, RoutedEventArgs e)
        {
            await Launcher.LaunchUriAsync(new Uri("http://item.taobao.com/item.htm?spm=686.1000925.1000774.10.vBDKsK&id=18235715158"));
        }
    }
}
